<div class="right company-data">
    <?
        $client = $data['Store']['Client'];
    ?>
	<h2><?= $client['name'] ?></h2>
	<p><?=  $client['address'] ?></p>
	<p>&nbsp;</p>
	<p class="list">
		<strong>PIB:</strong> <?= $client['pib'] ?><br>
		<strong>Matični broj:</strong> <?= $client['id_number'] ?><br>
		<strong>Broj računa:</strong> <?= $client['account'] ?>
	</p>
</div>
<div class="left company-data">
	<h2><?= $company_data['name'] ?></h2>
	<p><?=  $company_data['address'] ?>, <?= $company_data['zip'] ?> <?= $company_data['city'] ?></p>
	<p>Tel: <?=  $company_data['phone'] ?>; Mob: <?= $company_data['mobile'] ?></p>
	<p class="list">
		<strong>PIB:</strong> <?= $company_data['pib'] ?><br>
		<strong>Matični broj:</strong> <?= $company_data['id_number'] ?><br>
		<strong>Broj računa:</strong> <?= $company_data['account'] ?><br>
		<strong>Šifra delatnosti:</strong> <?= $company_data['industry_code'] ?>
	</p>
</div>

<p class="list left">
	<strong><?= mb_strtoupper($this->App->productionTypeLabel(@$data['Production']['type'])) ?>-FAKTURA BR:</strong> <b><?= @$data['Production']['number'] ?></b><br>
	<strong>Mesto izdavanja računa:</strong> <?= $company_data['city'] ?><br>
	<strong>Datum izdavanja računa:</strong> <?= local_date(@$data['Production']['date']) ?><br>
	<strong>Datum prometa:</strong> <?= local_date(@$data['Production']['date_trade']) ?>
</p>
<p class="list left second">
	<strong>Objekat:</strong> 
	<span class="left">
		<?= @$data['Store']['name'] ?><br>
		<?= @$data['Store']['address'] ?>
	</span><br clear="all" />
	
    <?php
        $paying_currency = 30;
        if (! empty($data['ClientPayingCurrency'][0])) {
            $paying_currency = (int)$data['ClientPayingCurrency'][0]['value'];
        }
        
        $payment_time = strtotime($data['Production']['date_trade']) + $paying_currency * 60 * 60 * 24;
        $payment_date = local_date(date('Y-m-d', $payment_time));
    ?>
	<strong>Rok za plaćanje:</strong> <?= $payment_date ?><br>
	<strong>Porudžbenica:</strong> <?= @$data['Production']['order_number'] ?>
</p>

<?
	$total_price = 0;
	$total_rabat = 0;
	$total_tax = 0;
?>
<br clear="all">
<table>
<tr>
	<th width="1%">Red. Broj</th>
	<th width="2%">Šifra</th>
	<th>Naziv artikla</th>
	<th width="1%" align="center">Kol.</th>
	<th width="1%" align="right">Cena</th>
	<th width="1%" align="right">Vrednost bez poreza</th>
	<th width="1%" align="right">Rabat</th>
	<th width="1%" align="right">Iznos Rabata</th>
	<th width="1%" align="right">Vrednost bez poreza i rabata</th>
	<th width="1%" align="right">Poreska stopa</th>
	<th width="1%" align="right">Porez</th>
	<th width="1%" align="right">Vrednost sa porezom</th>
</tr>
<? foreach($data['Product'] as $k=>$product): ?>
<?
	$amount		 = (float)$product['ProductionProduct']['amount'];
	$rabat		 = (int)@$product['ProductionProduct']['rabat'];
	$price 		 = (int)@$product['ProductionProduct']['price'];
	$price_rabat = $amount * $price * $rabat/100;
	$price_no_tax= $amount * $price - $price_rabat;
	$tax   		 = $data['Tax'][0]['value'];
	$price_tax	 = ($price_no_tax) * ($tax/100);
	
	$total_price += $amount * $price;
	$total_rabat += $price_rabat;
	$total_tax   += $price_tax;
	
	$code = sprintf("%04d", $product['id']);
	$client_codes = Set::combine($product['ProductClients'], '{n}.client_id', '{n}');
	$client_code  = @$client_codes[$data['Store']['client_id']]['code'];
	if(!empty($client_code)) {
		$code = $client_code;
	}
	
?>
<tr>
	<td><?= $k+1 ?>.</td>
	<td><?= $code ?></td>
	<td><strong><?= $product['name'] ?></strong></td>
	<td align="center"><?= $amount ?></td>
	<td align="right"><?= currency_format($price) ?></td>
	<td align="right"><?= currency_format($price * $amount) ?></td>
	<td align="right"><?=$rabat ?>%</td>
	<td align="right"><?= currency_format($price_rabat) ?></td>
	<td align="right"><?= currency_format($price_no_tax) ?></td>
	<td align="right"><?= $tax ?>%</td>
	<td align="right"><?= currency_format($price_tax) ?></td>
	<td align="right"><strong><?= currency_format($price_no_tax + $price_tax) ?></strong></td>
</tr>
<? endforeach; ?>
<tr class="subtotal">
    <td colspan="6" rowspan="6" valign="top" align="left">
        <p>
            <strong class="label" style="margin:0">Napomena o poreskom oslobađanju:</strong><br>
            <span style="display:block;margin-left:10pt"><?= empty($data['Production']['comment']) ? "-" : nl2br(@$data['Production']['comment']) ?></span>
        </p>
    </td>
	<td colspan="5">Vrednost bez poreza:</td>
	<td><?= currency_format($total_price) ?></td>
</tr>
<tr class="subtotal">
	<td colspan="5">Rabat:</td>
	<td><?= currency_format($total_rabat) ?></td>
</tr>
<tr class="subtotal">
	<td colspan="5">Vrednost bez poreza i rabata:</td>
	<td><?= currency_format($total_price-$total_rabat) ?></td>
</tr>
<tr class="subtotal">
	<td colspan="5">Porez:</td>
	<td><?= currency_format($total_tax) ?></td>
</tr>
<tr class="subtotal">
	<td colspan="5">Vrednost sa porezom:</td>
	<td><?= currency_format($total_price - $total_rabat + $total_tax) ?></td>
</tr>
<tr class="total">
    <td colspan="2"></td>
	<td class="total" colspan="3">SVEGA ZA UPLATU:</td>
	<td class="total"><?= currency_format($total_price - $total_rabat + $total_tax) ?></td>
</tr>
</table>

<div class="left tax-comment">
    
</div>

<div class="left clear">
    <div class="production-box">
        <span class="label span2">Odgovorno lice:</span>
        <span class="value"><?= @$company_data['resp_person_name'] ?>&nbsp;</span>
        
        <span class="label  span2">Telefon:</span>
        <span class="value"><?= @$company_data['resp_person_phone'] ?>&nbsp;</span>
        
        <span class="label span2">Br.Lične Karte:</span>
        <span class="value"><?= @$company_data['resp_person_id'] ?>&nbsp;</span>
        
        <span class="label span2">Matični broj:</span>
        <span class="value"><?= @$company_data['resp_person_number'] ?>&nbsp;</span>
    </div>
    
    <span class="label span2">Prodavac:</span><span class="potpis">&nbsp;</span>
    <span class="label span2">Magacioner:</span><span class="potpis">&nbsp;</span>
</div>
<div class="right">
    <span class="label span2">Prevoznik:</span>
    <span class="potpis"><?= @$data['Carrier']['name'] ?>&nbsp;</span>
    
    <span class="label  span2">Registarski broj:</span>
    <span class="potpis"><?= @$data['Vehicle']['plates'] ?>&nbsp;</span>
    
    <span class="label span2">Ime i Prezime:</span>
    <span class="potpis"><?= @$data['Driver']['name'] ?>&nbsp;</span>
    
    <span class="label span2">Br.Lične Karte:</span>
    <span class="potpis"><?= @$data['Driver']['id_number'] ?>&nbsp;</span>
    
    <span class="label span2">Kupac:</span><span class="potpis">&nbsp;</span>
</div>
<br clear="all">
<p>&nbsp;</p>